﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.Modules.Version.UserSource
{
    public partial class _edit : JumbotCms.API.UserCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Str2Str(q("id"));
            User_Load("", "html");
            checkRefer(id);
            if (!Page.IsPostBack)
            {
                this.ddlArmsId.Items.Clear();
                this.ddlArmsId.Items.Add(new ListItem("未涉及军种/兵种", "0"));
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[Title],[code]" +
                    " FROM [jcms_module_version_arms] ORDER BY code";
                DataTable dtArms = doh.GetDataTable();
                for (int i = 0; i < dtArms.Rows.Count; i++)
                {
                    this.ddlArmsId.Items.Add(new ListItem(getListName(dtArms.Rows[i]["Title"].ToString(), dtArms.Rows[i]["code"].ToString()), dtArms.Rows[i]["Id"].ToString()));
                }
                dtArms.Clear();
                dtArms.Dispose();
            }
            doh.Reset();
            string _nowtime = Jumbot.Common.staticFunction.GetNow("time");
            Jumbot.DataOper.WebFormHandler wh = new Jumbot.DataOper.WebFormHandler(doh, "jcms_module_version_source", btnSave);
            wh.AddBind(txtTitle, "Title", true);
            wh.AddBind(ddlArmsId, "ArmsId", false);
            wh.AddBind(txtSourceFrom, "SourceFrom", true);
            wh.AddBind(txtAuthor, "Author", true);
            wh.AddBind(txtAuthorSummary, "AuthorSummary", true);
            wh.AddBind(txtTags, "Tags", true);
            wh.AddBind(txtSummary, "Summary", true);
            wh.AddBind(txtCountries, "Countries", true);
            wh.AddBind(ref _nowtime, "AddDate", true);
            wh.AddBind(txtWordCount, "WordCount", false);
            wh.AddBind(txtDownUrl1, "DownUrl1", true);
            wh.ConditionExpress = "id=" + id;
            wh.Mode = Jumbot.DataOper.OperationType.Modify;
            wh.BindBeforeModifyOk += new EventHandler(bind_ok);
            wh.ModifyOk += new EventHandler(save_ok);
            wh.validator = chkForm;
        }
        /// <summary>
        /// 判断权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="_cType"></param>
        /// <returns></returns>
        protected void checkRefer(string id)
        {
            if (id == "0")
                FinalMessage("参数有误", site.Dir + "user/otherfiles/close.htm", 0);
            else
            {
                doh.Reset();
                doh.ConditionExpress = "id=" + id + " and UserId=" + UserId + " and State<3";
                if (!doh.Exist("jcms_module_version_source"))
                    FinalMessage("内容已经审核或你提交的参数有误", site.Dir + "user/otherfiles/close.htm", 0);
            }
        }
        /// <summary>
        /// 绑定数据后的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bind_ok(object sender, EventArgs e)
        {
            this.txtAuthorSummary.Text = Jumbot.Common.Strings.HtmlDecode(this.txtAuthorSummary.Text);
            this.txtSummary.Text = Jumbot.Common.Strings.HtmlDecode(this.txtSummary.Text);
        }
        protected bool chkForm()
        {
            if (!CheckFormUrl())
                return false;
            if (!Page.IsValid)
                return false;
            return true;
        }
        protected void save_ok(object sender, EventArgs e)
        {
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", id);
            doh.AddFieldItem("State", "2");
            doh.Update("jcms_module_version_source");
            FinalMessage("成功保存", site.Dir + "user/otherfiles/close.htm", 0);
        }
        /// <summary>
        /// 点击提交按钮后的一些操作
        /// 此事件是在数据正式入库前完成的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.txtAuthorSummary.Text = GetCutString(Jumbot.Common.Strings.HtmlEncode(this.txtAuthorSummary.Text), 1000);
            this.txtSummary.Text = GetCutString(Jumbot.Common.Strings.HtmlEncode(this.txtSummary.Text), 1000);
        }
    }
}
